const path = require('path');
const fs = require('fs');

const uuid = require('uuid').v1;
const { app, BrowserWindow } = require('electron');
const http = require("http");


const R = function (url) {
    if (url.indexOf('http') != -1) {
        return new Promise(function (resolve, reject) {
            try {
                http.get(url, function (data) {
                    var str = "";
                    data.on("data", function (chunk) {
                        str += chunk;//监听数据响应，拼接数据片段
                    })
                    data.on("end", function () {
                        resolve(JSON.parse(str)); //代码正常执行！
                    })
                });
            } catch (error) {
                reject(error)
            }
        })
    } else {
        return new Promise(function (resolve, reject) {
            try {
                var path = fs.readFileSync(url, 'utf-8');
                resolve(JSON.parse(path)); //代码正常执行！
            } catch (error) {
                reject(error)
            }
        });
    }
};
let winCache = {};
const isPack = app.isPackaged;
const plugInPath = path.join(__dirname, (isPack ? '../../' : './') + 'plug-in');

var join = function (_path, file) {
    return path.join(plugInPath, './' + _path + '/' + file)
};
fs.access(plugInPath, function (error, msg) {
    if (error) fs.mkdirSync(plugInPath)
});

module.exports = {
    get: function (errorCallback) {
        var plugInArray = fs.readdirSync(plugInPath);
        var plugInList = [];
        for (let i = 0; i < plugInArray.length; i++) {
            const element = plugInArray[i];
            var versionPath = path.join(plugInPath, element + '/config.json');
            try {
                var version = JSON.parse(fs.readFileSync(versionPath, 'utf-8'));
                version.path = element;
                version.id = uuid();
                version._icon = version.icon;
                version.icon = isPack ? '../../../plug-in/' + element + '/' + version.icon : '../plug-in/' + element + '/' + version.icon
                plugInList.push(version);
            } catch (error) {
                errorCallback(error);
            };
        };
        return plugInList;
    },
    getPackageStatus: () => isPack,
    open: function (data) {
        if (winCache[data.id]) {
            if (winCache[data.id].isMinimized()) winCache[data.id].restore()
            winCache[data.id].focus();
        } else {
            var iconPath = join(data.path, data._icon);
            var indexPath = join(data.path, data.index);
            let win = new BrowserWindow({
                height: data.minHeight,
                width: data.minWidth,
                minWidth: data.minWidth,
                minHeight: data.minHeight,
                autoHideMenuBar: true,
                show: false,
                icon: iconPath,
                webPreferences: {
                    nodeIntegration: true,
                    webSecurity: false
                }
            });
            win.once('ready-to-show', () => {
                win.show();
            });
            win.loadURL(indexPath);
            win.on('close', () => {
                delete winCache[data.id];
            })
            winCache[data.id] = win;
        }
    },
    close: function (id) {
        if (winCache[id]) {
            winCache[id].close();
        }
    }
};

